package com.iot08.admin.service;

import com.iot08.admin.service.page.PageVO;
import com.iot08.admin.service.page.PageableImpl;
import com.iot08.common.entity.admin.SysUser;
import com.iot08.common.entity.app.Questions;
import com.iot08.common.service.base.BaseService;
import com.iot08.common.utils.UUIDGenerator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 问题类别
 *
 * @Author: Weichao
 * @Date: 2024/09/02/16:49
 * @Description:
 */
@Service
public class QusetionsService extends BaseService<Questions> {

    @Resource
    private OperationService operationService;

    public List<Questions> onlyGetList(){
        return mongoTemplate.findAll(Questions.class);
    }
    public Page<Questions> list(PageVO page, SysUser user){
        PageableImpl pageable = new PageableImpl();
        page.setSort(Sort.by(Sort.Order.desc("create_time")));
        pageable.setPage(page);


        Criteria criteria = new Criteria();
        //按顺序 index 降序
//        criteria.and("index").gte(0);
        if (StringUtils.isNotEmpty(page.getSearchValue())) {
            criteria.and("category").regex(".*?" + page.getSearchValue() + ".*?", "i");
        }
        Query query = new Query().addCriteria(criteria);
        long count = mongoTemplate.count(query, Questions.class);
        page.setTotalResult((int) count);
        List<Questions> list = mongoTemplate.find(query.with(pageable), Questions.class);

        Page<Questions> result = new PageImpl<Questions>(list, pageable, count);
        return result;
    }

    public void add(Questions category, SysUser user, String ip){
        category.setId(UUIDGenerator.generate());
        category.setCategoryid(category.getId());
        category.setCreatetime(new java.util.Date());
        category.setModifytime(new java.util.Date());
        save(category);
        operationService.add(user, ip, null, "常见问题", null, "添加",
                String.format("常见问题-添加类别: %s ",category.getCategory()), null, null, null);

    }

    public void update(Questions category, SysUser user, String ip){
        Questions tempCategory = findById(category.getId());
        tempCategory.setModifytime(new java.util.Date());
        tempCategory.setCategory(category.getCategory());
        tempCategory.setIndex(category.getIndex());
        tempCategory.setCategoryid(category.getId());
        save(tempCategory);
        operationService.add(user, ip, null, "常见问题", null, "更新",
                String.format("常见问题-更新修改类别: %s ",category.getCategory()), null, null, null);
    }

    public boolean updateDel(String id, SysUser user, String ip){
        Questions category = findById(id);
        if(category == null){
            return false;
        }
        delById(id);
        operationService.add(user, ip, null, "常见问题", null, "删除",
                String.format("常见问题-删除类别: %s  %s",category.getCategory(), id), null, null, null);
        return true;
    }

}
